package com.zmhz.ctrl;

import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eova.common.Easy;
import com.eova.common.utils.EncryptUtil;
import com.eova.model.MetaObject;
import com.eova.service.sm;
import com.jfinal.core.Controller;
import com.jfinal.ext.kit.DateKit;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.zmhz.model.Frontusers;

public class FrontLoginController extends Controller {
	/**
	 * 前端用户登录
	 */
	public void login() {
		String json = HttpKit.readData(getRequest());
		JSONObject job = JSON.parseObject(json);
		
	}
	/**
	 * 前端用户注册
	 */
	public void reg() {
		String json = HttpKit.readData(getRequest());
		JSONObject job = JSON.parseObject(json);
		String phone = job.getString("phone");
		String pwd = job.getString("pwd");
		Integer usertype = job.getInteger("usertype");
		
		String sql = "select count(*) from frontusers where phone = ?";
		//Db.use(ac.object.getDs()).queryInt(sql, str2);
		int count = Db.queryInt(sql, phone);
		if(count>0) {
			renderJson(new Easy("该手机号"+phone+"已注册，请更换其它手机号，或联系客服！"));
		}
		
		Frontusers user = new Frontusers();
		user.set("phone", phone);
		user.set("pwd", EncryptUtil.getSM32(pwd));
		user.set("usertype", usertype);
		String token = EncryptUtil.getSM32(phone);
		user.set("token", token);
		String now = DateKit.toStr(new Date(), DateKit.timeStampPattern);
		user.set("regtime", now);//注册时间
		user.set("lasttime", now);//上次登录时间
		user.set("logintime", now);//本次登录时间
		user.set("userstatus", 0);//用户状态：注册用户
		user.set("errornum", 0);//登录
		
		Boolean flag = user.dao.save();
		Easy easy = new Easy(token);
		if(flag) {//注册成功
			//json = "{\"token\":\""+token+"\",\"userstatus\":0,\"usertype\":"+usertype+"}";
			easy.setSuccess(true);
		}else {
			easy.setMsg("服务器忙，注册失败！");
		}
		renderJson(easy);
	}
}
